module(..., package.seeall)
require "MD5"
require "nvm"
require "httpTask"
-- 参数配置文件初始化
nvm.init("config.lua", true)

local uploadVideoUrl = nvm.get("uploadVideoUrl")
local signKey = "bc503ef5853608c2a01f9077380ae2a6"
---组装接口发送
---@param params any 被排序的参数名集合
---@return non_empty_keys:非空参数集合, return_str_data=参数名=参数值&参数名=参数值
function NonEmptyParamNameSort(params)
    -- 获取非空值的参数名
    local non_empty_keys, return_str_data, bodyList = {}, "", ""
    for key, value in pairs(params) do
        if value ~= nil and value ~= "" then
            table.insert(non_empty_keys, key)
        end
    end
    print("non_empty_keys:", tostring(non_empty_keys))
    -- 对参数名进行排序
    table.sort(non_empty_keys)
    local total_keys = #non_empty_keys
    -- 输出排序后的参数名
    for index, key1 in ipairs(non_empty_keys) do
        if return_str_data == "" then
            if index == total_keys then
                return_str_data = key1 .. "=" .. params[key1] .. signKey
            else
                return_str_data = key1 .. "=" .. params[key1] .. "&"
            end
        else
            if index ~= total_keys then
                return_str_data =
                    return_str_data .. key1 .. "=" .. params[key1] .. "&"
            else
                return_str_data =
                    return_str_data .. key1 .. "=" .. params[key1] .. signKey
            end
        end
    end
    local sign = MD5.Md5Encryption(return_str_data)
    return sign
end
-- 服务码查询接口-根据设备编码
function GetServerObjectByClientNo(clientNo)
    local url = string.split(uploadVideoUrl, "/quake/uploadVideo")[1] ..
                    "/app/getServerObjectByClientNo"
    log.info("服务码查询ip：", url)
    httpTask.postApplicationJsonData(url, nil, {["clientNo"] = clientNo}, 6000,
                                     "getServerObjectByClientNo")
end
-- 获取系统时间
function GetSystemTime()
    local tClock, nowDate = os.date("*t"), ""
    local currentTime = os.clock() * 1000
    local milliseconds = math.floor(currentTime % 1000)
    nowDate = string.format("%04d-%02d-%02d %02d:%02d:%02d", tClock.year,
                            tClock.month, tClock.day, tClock.hour, tClock.min,
                            tClock.sec)
    nowDate = nowDate .. "." .. milliseconds
    return nowDate
end
-- 递归打印表的内容
function PrintTable(tbl, level, indent)
    level = level or 0
    indent = indent or 0

    -- 打印表的开始
    print(string.rep(" ", indent) .. "{")
    indent = indent + 2

    -- 遍历表中的每个键值对
    for key, value in pairs(tbl) do
        -- 打印键
        local keyStr = tostring(key)
        if type(key) == "string" then
            keyStr = string.format('"%s"', keyStr)
        end
        print(string.rep(" ", indent) .. "[" .. keyStr .. "] = ")

        -- 打印值
        if type(value) == "table" then
            -- 如果值是表，递归打印
            PrintTable(value, level + 1, indent)
        else
            -- 如果值不是表，直接打印
            if type(value) == "string" then
                print(string.format('"%s"', value))
            else
                print(tostring(value))
            end
        end
    end

    -- 打印表的结束
    indent = indent - 2
    print(string.rep(" ", indent) .. "}")
end
